<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;

class PartTimeJob extends ApiModel
{
    protected $table = 'part_time_jobs';

    protected $hidden = [
        'part_time_jobs_free_time', 'area_city', 'updated_at', 'town_city'
    ];

    protected $casts = [
        'id' => 'string', 'uid' => 'string', 'com_id' => 'string',
        'type_id' => 'string', 'long_term' => 'string',
        'need_num' => 'string', 'sex' => 'string', 'education' => 'string',
        'salary_price' => 'string', 'salary_unit' => 'string',
        'salary_method' => 'string', 'city_id' => 'string',
        'job_status' => 'string', 'count' => 'string'
    ];

    public function company_basic()
    {
        return $this->belongsTo(CompanyBasic::class, 'com_id')
            ->select(['id', 'company_name']);
    }

    /** 兼职时间表
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function part_time_jobs_free_time()
    {
        return $this->hasOne(PartTimeJobsFreeTime::class, 'bt_id');
    }

    /** 获取兼职时间
     * @return array
     */
    public function getFreeTimeAttribute()
    {
        //获取兼职时间表
        $time = $this->getRelationValue('part_time_jobs_free_time');
        if (is_null($time)) {
            return '';
        }

        $arr = [];
        for ($i = 1; $i < 22; $i++) {
            $key = 'num' . $i;
            $time->$key != 0 && $arr[] = $i;
        }
        return implode(',', $arr);
    }

    /** 兼职时间转为关联数组
     * @param $value
     * @return array
     */
    public static function changeFreeTime($value)
    {
        $value = is_array($value) ? $value : explode(',', $value);

        $arr = [];
        for ($i = 1; $i < 22; $i++) {
            $key = 'num' . $i;
            $arr[$key] = in_array($i, $value) ? 1 : 0;
        }
        return $arr;
    }

    /** 职位状态
     * @return mixed
     */
    public function getJobStatusNameAttribute()
    {
        $status = $this->attributes['job_status'];
        return ['正在审核', '审核通过', '已屏蔽', '已删除'][$status];
    }

    /** 兼职类型
     * @return mixed
     */
    public function getTypeIdNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['type_id'], 'part_time_type');
    }

    /** 付款单位
     * @return mixed
     */
    public function getSalaryUnitNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['salary_unit'], 'part_time_salary_unit');
    }

    /** 付款方式
     * @return mixed
     */
    public function getSalaryMethodNameAttribute()
    {
        return CollOption::getOptionName($this->attributes['salary_method'], 'part_time_salary_method');
    }

    /** 学历要求
     * @return string
     */
    public function getEducationNameAttribute()
    {
        if ($this->attributes['education'] == 0) {
            return '不限';
        }
        return CollOption::getOptionName($this->attributes['education'], 'education');
    }

    //-----------------------获取city_id城市名--------------------------
    /** area表
     * @return mixed
     */
    public function area_city()
    {
        return $this->hasOne(CollArea::class, 'cid', 'city_id')
            ->select('cid', 'name');
    }

    /** town表
     * @return mixed
     */
    public function town_city()
    {
        return $this->hasOne(CollTown::class, 'cid', 'city_id')
            ->select('cid', 'name');
    }

    /**
     * @return string
     */
    public function getCityIdNameAttribute()
    {
        $c_id = $this->attributes['city_id'];
        if ($c_id == 0) return '地区不限';

        if (strlen(strval($c_id)) > 7) {
            $c = $this->getRelationValue('town_city');
            if (empty($c) || strlen($c->name) < 1) return '地区不限';
            return $c->name;
        }

        $c = $this->getRelationValue('area_city');
        if (empty($c) || strlen($c->name) < 1) return '地区不限';
        return $c->name;
    }
    //-----------------------end------------------------------------------


    public function getUpdatetimeAttribute()
    {
        return substr($this->attributes['updated_at'], 0, 10);
    }

    public function getTimeStartAttribute($value)
    {
        return $value == '0000-00-00' ? '' : $value;
    }

    public function getTimeEndAttribute($value)
    {
        return $value == '0000-00-00' ? '' : $value;
    }

//    public function getTimeStartNameAttribute()
//    {
//        $t = $this->attributes['time_start'];
//        return $t == '0000-00-00' ? '' : substr($t, 0, 7);
//    }
//
//    public function getTimeEndNameAttribute()
//    {
//        $t = $this->attributes['time_end'];
//        return $t == '0000-00-00' ? '' : substr($t, 0, 7);
//    }
}
